﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RealtorsPortal.Data_Access_Layer;
using RealtorsPortal.Entities;
using RealtorsPortal.Business_Logic_Layer;

namespace RealtorsPortal.AdminCP
{
    public partial class ManageNews : System.Web.UI.Page
    {
        NewsDAL cDAL = new NewsDAL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadNewsList();
            }
        }

        protected void LoadNewsList()
        {
            NewsDAL NewsDAL = new NewsDAL();
            grdNewsList.DataSource = NewsDAL.getNewsData();
            grdNewsList.DataBind();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (this.uploadAImage())
            {
                News n = new News();

                n.UserId = (int)Session["adminId"];
                n.Title = txtTitle.Text;
                n.Content = txtContent.Text;
                n.NewsImage = "..\\Images\\" + fuNewsImage.FileName;
                n.WritedDate = DateTime.Today.ToShortDateString();
                if (AddNews(n))
                {
                    lblStatus.Text = "Successed";
                    LoadNewsList();
                }
                else
                {
                    lblStatus.Text = "Error, Check your typed in information";
                    LoadNewsList();
                }
            }

        }

        protected bool AddNews(News n)
        {
            if (cDAL.InsertNews(n))
            {
                return true;
            }
            else
            {
                return false;
            }
        }




        protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            grdNewsList.PageSize = int.Parse(ddlPageSize.Text);
            this.LoadNewsList();
        }


        protected bool uploadAImage()
        {
            lblStatus.Text = "";
            if (fuNewsImage.HasFile)
            {
                bool check = false;
                string[] end = { ".gif", ".jpeg", ".png", ".bmp", ".wmf", ".jpg", ".GIF", ".JPEG", ".PNG", ".BMP", ".WMF", "JPG" };
                foreach (string a in end)
                {
                    if (fuNewsImage.FileName.EndsWith(a))
                    {
                        check = true;
                    }
                }
                if (check)
                {
                    fuNewsImage.SaveAs(Server.MapPath("..\\Images") + "\\" + fuNewsImage.FileName);
                    return true;
                }
                else
                {
                    lblStatus.Text = "The upload file is not the image file. Please upload a image file";
                    return false;
                }

            }
            else
            {
                lblStatus.Text = "Please upload the image of the advertisement.";
                return false;
            }
        }

        protected void grdNewsList_RowCommand(object sender, GridViewCommandEventArgs e)
        {


            if (e.CommandName != null)
            {
                int index = Convert.ToInt32(e.CommandArgument);

                switch (e.CommandName)
                {
                    case "UpdateCommand":
                        UpdateNews(index);
                        grdNewsList.EditIndex = -1;
                        LoadNewsList();
                        break;
                    case "DeleteCommand":
                        int newsIndex = int.Parse(grdNewsList.DataKeys[index].Value.ToString());
                        
                        DeleteNews(newsIndex);
                        grdNewsList.EditIndex = -1;
                        LoadNewsList();
                        break;
                }
            }
        }


        protected bool UpdateNews(int RowIndex)
        {
            News c = new News();

            c.NewsId = int.Parse(grdNewsList.DataKeys[RowIndex].Value.ToString());
            c.Title = ((TextBox)grdNewsList.Rows[RowIndex].FindControl("txtTitle")).Text;
            c.Content = ((TextBox)grdNewsList.Rows[RowIndex].FindControl("txtContent")).Text;
            c.NewsImage = "..\\Images\\" + ((FileUpload)grdNewsList.Rows[RowIndex].FindControl("fuImage")).FileName;
            c.WritedDate = ((TextBox)grdNewsList.Rows[RowIndex].FindControl("txtDate")).Text;

            if (cDAL.UpdateNews(c))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        protected bool DeleteNews(int NewsIndex)
        {
            if (cDAL.DeleteNews(NewsIndex))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        protected void grdNewsList_RowEditing(object sender, GridViewEditEventArgs e)
        {
            grdNewsList.EditIndex = e.NewEditIndex;
            LoadNewsList();
        }

        protected void grdNewsList_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            grdNewsList.EditIndex = -1;
            LoadNewsList();
        }

        protected void grdNewsList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grdNewsList.PageIndex = e.NewPageIndex;
            LoadNewsList();
        }



    }
}